package com.shanjiduo.newsline;

import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.shanjiduo.newsline.fragment.ContentFragment;
import com.shanjiduo.newsline.fragment.HomeFragment;
import com.shanjiduo.newsline.fragment.MessageFragment;
import com.shanjiduo.newsline.fragment.MineFragment;
import com.shanjiduo.newsline.fragment.ReportFragment;
import com.shanjiduo.newsline.getui.PushUtils;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends SlidingFragmentActivity {
    public SlidingMenu menu;
    PushUtils pushUtils;
    private FragmentTabHost mTabHost;
    private ViewPager mViewPager;
    private List<Fragment> mFragmentList;
    private Class mClass[] = {HomeFragment.class, ReportFragment.class, MessageFragment.class, MineFragment.class};
    private Fragment mFragment[] = {HomeFragment.newInstance(Main2Activity.this), ReportFragment.newInstance(Main2Activity.this), new MessageFragment(), new MineFragment()};
    private String mTitles[] = {"首页", "视频", "关注", "我的"};
    private int mImages[] = {
            R.drawable.tab_home,
            R.drawable.tab_report,
            R.drawable.tab_message,
            R.drawable.tab_mine
    };
    private FrameLayout fr;

    @Override
    public void onCreate(Bundle savedInstanceState) {//修改了权限
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        init();
        pushUtils = new PushUtils(this);//集成个推

    }

    /**
     * 权限申请结果处理
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PushUtils.REQUEST_PERMISSION) {
            if ((grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
                pushUtils.initPush();
            } else {
                Log.e(getLocalClassName(), "We highly recommend that you need to grant the special permissions before initializing the SDK, otherwise some "
                        + "functions will not work");
                pushUtils.initPush();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void init() {
        initMenu();
        initView();

        //initEvent();
    }

    private void initMenu() {
        setBehindContentView(R.layout.left_menu);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.menu_frame, ContentFragment.newInstance("", ""))
                .commit();


        menu = getSlidingMenu();
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
   /*
       HomeFragment hf= new HomeFragment();
        Bundle bu=new Bundle();
        bu.putSerializable("sliding",new SlidingFragmentActivity());*/

    }

    private void initView() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        //  fr = (FrameLayout) findViewById(R.id.view_pager);
        //  mFragmentList = new ArrayList<Fragment>();

        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        mTabHost.getTabWidget().setDividerDrawable(null);

        for (int i = 0; i < mFragment.length; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTitles[i]).setIndicator(getTabView(i));
            mTabHost.addTab(tabSpec, mClass[i], null);
            //   mFragmentList.add(mFragment[i]);
            mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
        }

       /* mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        });*/
    }

    private View getTabView(int index) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);

        ImageView image = (ImageView) view.findViewById(R.id.image);
        TextView title = (TextView) view.findViewById(R.id.title);

        image.setImageResource(mImages[index]);
        title.setText(mTitles[index]);

        return view;
    }

    private void initEvent() {

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                mViewPager.setCurrentItem(mTabHost.getCurrentTab());
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                mTabHost.setCurrentTab(position);
                //解决SlidingMenu与ViewPager冲突
                switch (position) {
                    case 0:
                        Main2Activity.this.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    default:

                        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
                        break;
                }
            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


}
